#!/usr/bin/python
#Auther : MillionLee

'''
OAuth ops for weibo


'''

import unittest
from weibopy.auth import OAuthHandler
from weibopy.api import API

from config import *

class GetOAuth(unittest.TestCase):
    
    consumer_key = OAUTH_CONSUMER_KEY_WEIBO
    consumer_secret = OAUTH_CONSUMER_SECRET_WEIBO
    
    def __init__(self):
            """ constructor """
        
    def authInCmdline(self):
        self.auth = OAuthHandler(self.consumer_key, self.consumer_secret)
        auth_url = self.auth.get_authorization_url()
        print 'Please authorize: ' + auth_url
        verifier = raw_input('PIN: ').strip()
        self.auth.get_access_token(verifier)
        self.api = API(self.auth)
        
    def getAuthUrl(self, callback = None):
        #until 10/09/22, weibo py lib do not support callback
        self.auth = OAuthHandler(self.consumer_key, self.consumer_secret)
        auth_url = self.auth.get_authorization_url()
        if callback == None:
            return auth_url
        else:
            return auth_url + "&oauth_callback=" + callback
    
    def getAccessToken(self, verifier, request_token_key, request_token_secret):
        self.auth = OAuthHandler(self.consumer_key, self.consumer_secret)
        self.auth.set_request_token(request_token_key, request_token_secret)
        return self.auth.get_access_token(verifier = verifier)
    
    def getUsername(self):
        return self.auth.get_username()

test = GetOAuth()
print test.getAuthUrl()

